public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;
    public int size(){
        int len = 0;
        ListNode cur = head;
        while (cur != null){
            len++;
            cur = cur.next;
        }
        return len;
    }
    public void addFirst(int date){
        ListNode node = new ListNode(date);
        if (head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }
    public void addLast(int date){
        ListNode node = new ListNode(date);
        if (head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    public void addIndex(int index,int date){
        if (index<0||index > size()){
            System.out.println("下标异常");
        }
        if (index == 0){
            addFirst(date);
            return;
        }
        if (index == size()){
            addLast(date);
            return;
        }
        ListNode cur = new ListNode(index);
        ListNode node = new ListNode(date);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }
}
